layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;
  var deviceSn = $('input[name="deviceSn"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/device/slot/list.data'
      ,where:{deviceSn:deviceSn}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'slotNum', title: '仓室号'}
      ,{field: 'portableBatterySn', title: '电池ID'}
      ,{field: 'portableBatteryPower', title: '电池电量',templet:function (record){
          if(record.portableBattery){
              return record.portableBattery.power;
          }
          return '--';
      }}
      ,{field: 'portableBatteryState', title: '电池状态',templet:function (record){
          if(record.portableBattery){
              return record.portableBattery.state;
          }
          return '--';
      }}
      ,{field: 'stateName', title: '仓室状态'}
      ,{field: 'updateDate', title: '最后修改时间'}
      ,{title: '操作', width: 200, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,text: '对不起，加载出现异常！'
  });
  
  //事件
  var active = {
      search:function(){
          layer.confirm('确定要发送查询命令吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/operate/search.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('查询成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,autoLease: function(){
          layer.open({
              type: 2
              ,title: '自动租借'
              ,content: layui.setter.webRoot + '/bxscn/device/operate/lease.html?deviceSn='+deviceSn
              ,maxmin: true
              ,area: ['500px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/operate/lease.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table'); //数据刷新
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,lease: function(obj){
          layer.open({
              type: 2
              ,title: '指定仓室租借'
              ,content: layui.setter.webRoot + '/bxscn/device/operate/lease.html?deviceSn='+deviceSn+'&slotNum='+obj.data.slotNum
              ,maxmin: true
              ,area: ['500px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit',submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/operate/lease.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table'); //数据刷新
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,autoOpenSlot: function(){
          layer.confirm('确定要随机打开空仓室吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/operate/open_slot.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('开仓成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,openSlot: function(obj){
          layer.confirm('确定要打开'+obj.data.slotNum+'仓室吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/operate/open_slot.do',
                  data:{deviceSn:deviceSn,slotNum:obj.data.slotNum},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('开仓成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,ejectAll:function(){
          layer.confirm('确定要强制打开全部仓室吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/operate/eject_all.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('开仓成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,eject:function(obj){
          layer.confirm('确定要强制开仓吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/operate/eject.do',
                  data:{deviceSn:deviceSn,slotNum:obj.data.slotNum},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('开仓成功',{icon: 1},function (){
                              table.reload('LAY-table'); //数据刷新
                          },5000);
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('portable_energy_cabinet_info', {})
});